package xyz.xuminghai.javafx_learn.scene.effect;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.control.Tooltip;
import javafx.scene.effect.ColorAdjust;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

import java.io.FileNotFoundException;
import java.net.URL;

/**
 * 2023/7/21 12:28 星期五<br/>
 * 允许按像素调整色调、饱和度、亮度和对比度的效果
 *
 * @author xuMingHai
 */
public class ColorAdjustApp extends Application {

    private final BorderPane root = new BorderPane();

    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void init() throws Exception {
        String fileName = "color-adjust-app.png";
        URL url = getClass().getResource(fileName);
        if (url == null) {
            throw new FileNotFoundException(fileName);
        }

        Image image = new Image(url.toExternalForm(),
                500.0, 300.0, true, true);
        ImageView imageView = new ImageView(image);
        ColorAdjust colorAdjust = new ColorAdjust();
        imageView.setEffect(colorAdjust);

        VBox vBox = new VBox(30.0,
                brightnessParameter(colorAdjust),
                contrastParameter(colorAdjust),
                hueParameter(colorAdjust),
                saturationParameter(colorAdjust));
        vBox.setAlignment(Pos.CENTER);

        root.setCenter(imageView);
        root.setRight(vBox);

    }

    private HBox brightnessParameter(ColorAdjust colorAdjust) {
        Label label = new Label("Brightness");
        label.setFont(Font.font(20.0));
        label.setTooltip(new Tooltip("""
                亮度调整值。
                         Min: -1.0
                         Max: +1.0
                     Default:  0.0
                    Identity:  0.0
                默认值 0.0
                """));

        Slider slider = new Slider(-1.0, 1.0, 0.0);
        // 建立绑定
        colorAdjust.brightnessProperty().bind(slider.valueProperty());

        return new HBox(20.0, label, slider);
    }

    private HBox contrastParameter(ColorAdjust colorAdjust) {
        Label label = new Label("Contrast");
        label.setFont(Font.font(20.0));
        label.setTooltip(new Tooltip("""
                对比度调整值。
                        Min: -1.0
                        Max: +1.0
                    Default:  0.0
                   Identity:  0.0
                默认值 0.0
                """));

        Slider slider = new Slider(-1.0, 1.0, 0.0);
        // 建立绑定
        colorAdjust.contrastProperty().bind(slider.valueProperty());

        return new HBox(20.0, label, slider);
    }

    private HBox hueParameter(ColorAdjust colorAdjust) {
        Label label = new Label("Hue");
        label.setFont(Font.font(20.0));
        label.setTooltip(new Tooltip("""
                色调调整值。
                         Min: -1.0
                         Max: +1.0
                     Default:  0.0
                    Identity:  0.0
                默认值 0.0
                """));

        Slider slider = new Slider(-1.0, 1.0, 0.0);
        // 建立绑定
        colorAdjust.hueProperty().bind(slider.valueProperty());

        return new HBox(20.0, label, slider);
    }

    private HBox saturationParameter(ColorAdjust colorAdjust) {
        Label label = new Label("Saturation");
        label.setFont(Font.font(20.0));
        label.setTooltip(new Tooltip("""
                饱和度调整值。
                        Min: -1.0
                        Max: +1.0
                    Default:  0.0
                   Identity:  0.0
                默认值 0.0
                """));

        Slider slider = new Slider(-1.0, 1.0, 0.0);
        // 建立绑定
        colorAdjust.saturationProperty().bind(slider.valueProperty());

        return new HBox(20.0, label, slider);
    }


    public void start(Stage primaryStage) {
        primaryStage.setTitle(getClass().getSimpleName());
        Scene scene = new Scene(root, 800, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
